#import "RNSearchSubheaderCell.h"
#import "RNConstants.h"
#import "NSShadow+MCAdditions.h"

static NSUInteger const titlePadding = 5;

@implementation RNSearchSubheaderCell

@synthesize titleAttributesNormal = titleAttributesNormal_;

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [[self attributedStringValue] drawInRect:[self titleRectForBounds:cellFrame]];
}

-(NSRect)titleRectForBounds:(NSRect)cellFrame {
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:[self titleAttributesNormal]];
    NSSize titleSize = [title size];
    NSRect titleRect = NSInsetRect(cellFrame, (NSWidth(cellFrame) - titleSize.width) / 2, 0);
    titleRect.origin.y += NSHeight(cellFrame) - titleSize.height - titlePadding;
    titleRect.size.height = [title size].height;
    return titleRect;
}


#pragma mark -
#pragma mark Accessors

-(BOOL)isEditable {
    return NO;
}

-(BOOL)isSelectable {
    return NO;
}

-(void)setObjectValue:(id)obj {
    if (![obj isKindOfClass:[NSDictionary class]])
        return;
    [super setObjectValue:[obj valueForKey:RNTitleKey]];
}

-(NSDictionary *)titleAttributesNormal {
    if (!titleAttributesNormal_) {
        NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
        [attributes setValue:[NSFont systemFontOfSize:12.0f] forKey:NSFontAttributeName];
        [attributes setValue:[NSColor colorWithCalibratedWhite:0.35f alpha:1.0f] forKey:NSForegroundColorAttributeName];
        NSColor *shadowColor = [NSColor colorWithCalibratedWhite:0.87f alpha:1.0f];
        NSShadow *textShadow = [[NSShadow alloc] initWithColor:shadowColor offset:NSMakeSize(0, -1) blurRadius:1];
        [attributes setValue:textShadow forKey:NSShadowAttributeName];
        titleAttributesNormal_ = (NSDictionary *)attributes;
    }
    return titleAttributesNormal_;
}

- (NSAttributedString *)attributedStringValue {
    return [[NSAttributedString alloc] initWithString:[self stringValue] attributes:[self titleAttributesNormal]];
}

@end
